home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / layerEditor.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  61.2 KB  |  2,275 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  08 Nov 2000
  22. //  Author:         bwk
  23. //
  24. //  Description:
  25. //      This script initializes the Channel Box and Layer Editor.  
  26. //        Initialization involves determining the initial preferences, 
  27. //        creating the UI and setting the initial visibility.
  28. //
  29.  
  30. proc createLayerEditor(string $parent)
  31. //
  32. //    Description:
  33. //        Create the Layer Editor UI.
  34. //
  35. //    Arguments:
  36. //        $parent - The parent layout. Must be a form layout.
  37. //
  38. {
  39.     global string $gCurrentLayerEditor;
  40.  
  41.     //    Bail if the parent layout is not the expected type.
  42.     //
  43.     string $requiredType = "formLayout";
  44.     if ($requiredType != `objectTypeUI $parent`) {
  45.         error ("Argument must be a " + $requiredType);
  46.         return;
  47.     }
  48.  
  49.     setParent $parent;
  50.     $gCurrentLayerEditor = $parent;
  51.     
  52.     //    Need a menu bar layout for the menus.
  53.     //
  54.     $menuBarLayout = `menuBarLayout`;
  55.  
  56.     //    Create Edit menu and items.
  57.     //
  58.     $editMenu = `menu -label "Layers"`;
  59.     $newMenuItem = `menuItem -label "Create Layer"`;
  60.     $deleteMenuItem = `menuItem -label "Delete Selected Layer(s)"
  61.         LayerEditorEditMenuDeleteMenuItem`;
  62.     $layerMenuItem = `menuItem -label "Edit Selected Layer(s)..."
  63.         LayerEditorEditMenuEditMenuItem`;
  64.  
  65.     menuItem -divider true;
  66.     $selectObjectsMenuItem = `menuItem -label "Select Objects in Selected Layer(s)"
  67.         LayerEditorEditMenuSelectObjectsMenuItem`;
  68.     $addObjectsMenuItem = `menuItem -label "Add Selected Objects to Current Layer"
  69.         LayerEditorEditMenuAddObjectsMenuItem`;
  70.     $removeObjectsMenuItem = `menuItem -label "Remove Objects from Selected Layer(s)"
  71.         LayerEditorEditMenuRemoveObjectsMenuItem`;
  72.  
  73.     menuItem -divider true;
  74.     $layerAttributesMenuItem = `menuItem -label "Layer Attributes..."
  75.         LayerEditorEditMenuLayerAttributesMenuItem`;
  76.     $membershipMenuItem = `menuItem -label "Membership..."`;
  77.  
  78.     menuItem -divider true;
  79.     $selectUnusedMenuItem = `menuItem -label "Select Unused Layers"`;
  80.     $removeFromLayerMenuItem = `menuItem -label "Remove Selected Object(s) from Layers"
  81.         LayerEditorEditMenuRemoveFromLayerMenuItem`;
  82.  
  83.     //    Create Option menu and items.
  84.     //
  85.     $optionsMenu = `menu -label "Options"`;
  86.     menuItem -checkBox false -label "Make New Layers Current"
  87.         -command ("optionVar -intValue displayLayerCurrent #1;"
  88.             + "optionVar -intValue renderLayerCurrent #1;")
  89.         LayerEditorOptionsMenuMakeCurrentMenuItem;
  90.     menuItem -checkBox false -label "Use Current Layer"
  91.         -command ("editDisplayLayerGlobals -useCurrent #1;"
  92.             + "editRenderLayerGlobals -useCurrent #1;")
  93.         LayerEditorOptionsMenuUseCurrentMenuItem;
  94.  
  95.     //    Create form layout for rest of editor.
  96.     //
  97.     $form = `formLayout`;
  98.  
  99.     //    Option menu for displaying Display Layers versus Render Layers.
  100.     //
  101.     $optionMenu = `optionMenu LayerEditorTypeOptionMenu`;
  102.     menuItem -label "Display";
  103.     menuItem -label "Render";
  104.  
  105.     //    Button for creating new layers.
  106.     //
  107.     $newButton = `symbolButton -image "newlayer.xpm"
  108.         -annotation "Create a new layer"`;
  109.  
  110.     //    Tab layout with two children. One for display layers, the other
  111.     //    for render layers.
  112.     //
  113.     setParent $form;
  114.     string $tabLayout = `tabLayout -scrollable false -tabsVisible false
  115.         DisplayLayerUITabLayout`;
  116.  
  117.     //    Display layers.
  118.     //
  119.     setParent $tabLayout;
  120.     scrollLayout -horizontalScrollBarThickness 0 DisplayLayerTab;
  121.     gridLayout
  122.         -allowEmptyCells     false
  123.         -autoGrow            true
  124.         -numberOfColumns     1
  125.         -cellWidthHeight     350 20
  126.         LayerEditorDisplayLayerLayout;
  127.  
  128.     //    Render layers.
  129.     //
  130.     setParent $tabLayout;
  131.     scrollLayout -horizontalScrollBarThickness 0 RenderLayerTab;
  132.     gridLayout
  133.         -allowEmptyCells     false
  134.         -autoGrow            true
  135.         -numberOfColumns     1
  136.         -cellWidthHeight     350 20
  137.         LayerEditorRenderLayerLayout;
  138.  
  139.     optionMenu -edit
  140.         -changeCommand ("layerEditorDisplayTypeChange")
  141.         $optionMenu;
  142.  
  143.     formLayout -edit
  144.         -attachForm     $optionMenu    "top"    1
  145.         -attachForm     $optionMenu    "left"   1
  146.         -attachNone     $optionMenu    "bottom"
  147.         -attachControl  $optionMenu    "right"  0 $newButton
  148.  
  149.         -attachForm     $newButton     "top"    1
  150.         -attachNone     $newButton     "left"
  151.         -attachNone     $newButton     "bottom"
  152.         -attachForm     $newButton     "right"  1
  153.  
  154.         -attachControl  $tabLayout     "top"    2 $optionMenu
  155.         -attachForm     $tabLayout     "left"   0
  156.         -attachForm     $tabLayout     "bottom" 0
  157.         -attachForm     $tabLayout     "right"  0
  158.         $form;
  159.  
  160.     formLayout -edit
  161.         -attachForm     $menuBarLayout "top"    0
  162.         -attachForm     $menuBarLayout "left"   0
  163.         -attachForm     $menuBarLayout "bottom" 0
  164.         -attachForm     $menuBarLayout "right"  0
  165.         $parent;
  166.  
  167.     //    Attach commands to menu items and buttons.
  168.     //
  169.     menu -edit -postMenuCommand ("layerEditorShowEditMenu " + $editMenu) $editMenu;
  170.     menu -edit -postMenuCommand ("layerEditorShowOptionsMenu " + $optionsMenu) $optionsMenu;
  171.  
  172.     menuItem -edit -command ("layerEditorCreateLayer") $newMenuItem;
  173.     menuItem -edit -command ("layerEditorDeleteLayer \"\"") $deleteMenuItem;
  174.     menuItem -edit -command ("layerEditorEditLayer \"\"") $layerMenuItem;
  175.     menuItem -edit -command ("layerEditorSelectObjects \"\"") $selectObjectsMenuItem;
  176.     menuItem -edit -command ("layerEditorAddObjects \"\"") $addObjectsMenuItem;
  177.     menuItem -edit -command ("layerEditorRemoveObjects \"\"") $removeObjectsMenuItem;
  178.     menuItem -edit -command ("layerEditorSelectUnused") $selectUnusedMenuItem;
  179.     menuItem -edit -command ("layerEditorRemoveFromLayer") $removeFromLayerMenuItem;
  180.     menuItem -edit -command ("layerEditorLayerAttributes \"\"") $layerAttributesMenuItem;
  181.     menuItem -edit -command ("layerEditorMembership \"\"") $membershipMenuItem;
  182.  
  183.     symbolButton -edit -command ("layerEditorCreateLayer") $newButton;
  184.  
  185.     //    Attach script jobs for detecting when user creates new scene 
  186.     //    or opens a scene.
  187.     //
  188.     scriptJob -permanent -parent $parent -event NewSceneOpened ("layerEditorNewScene");
  189.     scriptJob -permanent -parent $parent -event SceneOpened ("layerEditorOpenScene");
  190.  
  191.     //    Attach a script job that detects creation/deletion of layer nodes.
  192.     //
  193.     scriptJob -permanent -parent $parent -event displayLayerChange ("layerEditorDisplayLayerChange");
  194.     scriptJob -permanent -parent $parent -event renderLayerChange ("layerEditorRenderLayerChange");
  195. }
  196.  
  197. proc string getShortName(string $name)
  198. //
  199. //    Description:
  200. //        Given a full path name to an object return the short name.
  201. //
  202. //    Arguments:
  203. //        $name - Full path name to an object.
  204. //
  205. //    Returns:
  206. //        The short name.
  207. //
  208. {
  209.     string $result = "", $tokenArray[];
  210.     int    $tokenCount;
  211.  
  212.     //    To get the short name from a long name tokenize the long name
  213.     //    using "|" as the split character because that is the path
  214.     //    separator. The short name is simply the last token.
  215.     //
  216.     $tokenCount = `tokenize $name "|" $tokenArray`;
  217.     if (0 < $tokenCount) $result = $tokenArray[$tokenCount - 1];
  218.  
  219.     return $result;
  220. }
  221.  
  222. proc int isValidName(string $name)
  223. //
  224. //    Description:
  225. //        Determine if the given string is potentially a valid name.
  226. //        Note that this procedure does not test to see if the name is
  227. //        unique, it just tests the string characters.
  228. //
  229. //        Check to ensure that the string does not begin with a number
  230. //        and is followed only by alphanumeric characters or underscores.
  231. //
  232. //    Arguments:
  233. //        $name - The name.
  234. //
  235. //    Returns:
  236. //        True if the name is valid, false otherwise.
  237. //
  238. {
  239.     int $result = false;
  240.  
  241.     if ("" != $name) {
  242.         //
  243.         //    Begins with letter or underscore, followed by
  244.         //    letters, digits, or underscores.
  245.         //
  246.         string $regExpr = "([a-zA-Z_]+)([[a-zA-Z0-9_])*";
  247.         string $temp = match($regExpr, $name);
  248.         if ($temp == $name) {
  249.             $result = true;
  250.         }
  251.     }
  252.  
  253.     return $result;
  254. }
  255.  
  256. proc string getLayerDisplayType()
  257. //
  258. //    Description:
  259. //        Return the current type of layer being displayed in the 
  260. //        Layer Editor.
  261. //
  262. //    Returns:
  263. //        "Display" or "Render".
  264. //
  265. {
  266.     global string $gCurrentLayerEditor;
  267.  
  268.     string $type = "";
  269.  
  270.     //    Verify the current layer editor.
  271.     //
  272.     if ("" == $gCurrentLayerEditor) {
  273.         error ("There is no current Layer Editor");
  274.         return $type;
  275.     }
  276.  
  277.     //    Set the parent to the current layer editor. We can now use short
  278.     //    names of our controls and be certain they reference the correct
  279.     //    control.
  280.     //
  281.     setParent $gCurrentLayerEditor;
  282.  
  283.     //    Get the current type of layers being displayed.
  284.     //
  285.     string $type = `optionMenu -query -value LayerEditorTypeOptionMenu`;
  286.  
  287.     if ("Display" == $type) {
  288.     } else if ("Render" == $type) {
  289.     } else {
  290.         error ("Invalid layer type: " + $type);
  291.     }
  292.  
  293.     return $type;
  294. }
  295.  
  296. proc string [] getLayerSelection(string $type)
  297. //
  298. //    Description:
  299. //        Return the current layer selection for the specified layer type.
  300. //
  301. //    Arguments:
  302. //        $type - Valid values are: "Display", "Render", or
  303. //                an emtpy string. In the case of an empty string the
  304. //                current layer type displayed in the editor will be used.
  305. //
  306. //    Returns:
  307. //        A string array containing the names of the selected layers.
  308. //
  309. {
  310.     global string $gCurrentLayerEditor;
  311.  
  312.     string $selectionArray[];
  313.     string $buttonLayout = "", $button, $buttonArray[];
  314.     int    $index = 0;
  315.  
  316.     //    If type argument not specified then determine it.
  317.     //
  318.     if ("" == $type) {
  319.         $type = getLayerDisplayType();
  320.     }
  321.  
  322.     //    Set the parent to the current layer editor. We can now use short
  323.     //    names of our controls and be certain they reference the correct
  324.     //    control.
  325.     //
  326.     setParent $gCurrentLayerEditor;
  327.  
  328.     //    Get the correct layout of layer buttons depending on the type of
  329.     //    layers currently being shown.
  330.     //
  331.     if ("Display" == $type) {
  332.         $buttonLayout = "LayerEditorDisplayLayerLayout";
  333.  
  334.     } else if ("Render" == $type) {
  335.         $buttonLayout = "LayerEditorRenderLayerLayout";
  336.  
  337.     } else {
  338.         error ("Invalid layer type: " + $type);
  339.         return $selectionArray;
  340.     }
  341.  
  342.     //    Get all the buttons in the layout.
  343.     //
  344.     $buttonArray = `layout -query -childArray $buttonLayout`;
  345.  
  346.     //    For each button check to see if it is selected.
  347.     //
  348.     for ($button in $buttonArray) {
  349.         if (("Display" == $type && `layerButton -query -select $button`)
  350.             || ("Render" == $type && `renderLayerButton -query -select $button`)) {
  351.             $selectionArray[$index++] = $button;
  352.         }
  353.     }
  354.  
  355.     return $selectionArray;
  356. }
  357.  
  358. proc string getCurrentLayer(string $type)
  359. //
  360. //    Description:
  361. //        Return the current layer for the specified layer type.
  362. //
  363. //    Arguments:
  364. //        $type - Valid values are: "Display", "Render", or
  365. //                an emtpy string. In the case of an empty string the
  366. //                current layer type displayed in the editor will be used.
  367. //
  368. //    Returns:
  369. //        A string containing the name of the current layer.
  370. //
  371. {
  372.     global string $gCurrentLayerEditor;
  373.  
  374.     string $currentLayer = "";
  375.     string $buttonLayout = "", $button, $buttonArray[];
  376.  
  377.     //    If type argument not specified then determine it.
  378.     //
  379.     if ("" == $type) {
  380.         $type = getLayerDisplayType();
  381.     }
  382.  
  383.     //    Set the parent to the current layer editor. We can now use short
  384.     //    names of our controls and be certain they reference the correct
  385.     //    control.
  386.     //
  387.     setParent $gCurrentLayerEditor;
  388.  
  389.     //    Get the correct layout of layer buttons depending on the type of
  390.     //    layers currently being shown.
  391.     //
  392.     if ("Display" == $type) {
  393.         $buttonLayout = "LayerEditorDisplayLayerLayout";
  394.  
  395.     } else if ("Render" == $type) {
  396.         $buttonLayout = "LayerEditorRenderLayerLayout";
  397.  
  398.     } else {
  399.         error ("Invalid layer type: " + $type);
  400.         return $currentLayer;
  401.     }
  402.  
  403.     //    Get all the buttons in the layout.
  404.     //
  405.     $buttonArray = `layout -query -childArray $buttonLayout`;
  406.  
  407.     //    For each button check to see if it is the current layer.
  408.     //
  409.     for ($button in $buttonArray) {
  410.         if (("Display" == $type && `layerButton -query -current $button`)
  411.             || ("Render" == $type && `renderLayerButton -query -current $button`)) {
  412.             $currentLayer = $button;
  413.             break;
  414.         }
  415.     }
  416.  
  417.     return $currentLayer;
  418. }
  419.  
  420. proc removeAllLayerButtons()
  421. //
  422. //    Description:
  423. //        Remove all the layer buttons in the Layer Editor.
  424. //
  425. //        This method needs to be (and should only be) called when a scene is
  426. //        opened or a new scene is created.
  427. //
  428. {
  429.     global string $gCurrentLayerEditor;
  430.  
  431.     if ("" == $gCurrentLayerEditor) {
  432.         error ("There is no current Layer Editor");
  433.         return;
  434.     }
  435.  
  436.     setParent $gCurrentLayerEditor;
  437.  
  438.     string $button, $buttonArray[], $buttonLayout;
  439.  
  440.     //    Delete all the display layer buttons.
  441.     //
  442.     $buttonLayout = "LayerEditorDisplayLayerLayout";
  443.     $buttonArray = `layout -query -childArray $buttonLayout`;
  444.     for ($button in $buttonArray) {
  445.         deleteUI $button;
  446.     }
  447.  
  448.     //    Delete all the render layer buttons.
  449.     //
  450.     $buttonLayout = "LayerEditorRenderLayerLayout";
  451.     $buttonArray = `layout -query -childArray $buttonLayout`;
  452.     for ($button in $buttonArray) {
  453.         deleteUI $button;
  454.     }
  455. }
  456.  
  457. global proc string[] sortLayers(string $layerArray[])
  458. //
  459. //    Description:
  460. //        Sort the layers in the argument array according to their
  461. //        display order attribute.
  462. //
  463. //    Arguments:
  464. //        $layerArray[] - A string array of layer names.
  465. //
  466. //    Returns:
  467. //        A string array containing the sorted list of layers.
  468. //
  469. {
  470.     string $result[];
  471.     int    $count, $index, $position, $positionArray[];
  472.     
  473.     //    Construct an array consisting of each layer's display order
  474.     //    attribute value.
  475.     //
  476.     $count = size($layerArray);
  477.     for ($index = 0; $index < $count; $index++) {
  478.         $position = `getAttr ($layerArray[$index] + ".displayOrder")`;
  479.         $positionArray[$index] = $position;
  480.     }
  481.  
  482.     //    Now sort the display order array.
  483.     //
  484.     $positionArray = sort($positionArray);
  485.  
  486.     //    Construct the result array by matching the layer with it's
  487.     //    corresponding display order value.
  488.     //
  489.     for ($index = 0; $index < $count; $index++) {
  490.         for ($layerIndex = 0; $layerIndex < $count; $layerIndex++) {
  491.             if ("" != $layerArray[$layerIndex]) {
  492.                 $position = `getAttr ($layerArray[$layerIndex] + ".displayOrder")`;
  493.                 if ($positionArray[$index] == $position) {
  494.                     $result[$index] = $layerArray[$layerIndex];
  495.                     $layerArray[$layerIndex] = "";
  496.                     break;
  497.                 }
  498.             }
  499.         }
  500.     }
  501.  
  502.     return $result;
  503. }
  504.  
  505. global proc layerEditorNewScene()
  506. //
  507. //    Description:
  508. //        This procedure is called whenever the user creates a new scene.
  509. //
  510. //        Update the Layer Editor to reflect any layer changes.
  511. //
  512. {
  513.     removeAllLayerButtons();
  514.     updateLayerEditor();
  515. }
  516.  
  517. global proc layerEditorOpenScene()
  518. //
  519. //    Description:
  520. //        This procedure is called whenever the user opens a new scene.
  521. //
  522. //        Update the Layer Editor to reflect any layer changes.
  523. //
  524. {
  525.     removeAllLayerButtons();
  526.     updateLayerEditor();
  527. }
  528.  
  529. global proc layerEditorDisplayLayerChange()
  530. //
  531. //    Description:
  532. //        This procedure is called whenever the user creates or deletes
  533. //        a display layer.
  534. //
  535. //        Update the Layer Editor to reflect the layer changes.
  536. //
  537. {
  538.     updateLayersByType("Display");
  539. }
  540.  
  541. global proc layerEditorRenderLayerChange()
  542. //
  543. //    Description:
  544. //        This procedure is called whenever the user creates or deletes
  545. //        a render layer.
  546. //
  547. //        Update the Layer Editor to reflect the layer changes.
  548. //
  549. {
  550.     updateLayersByType("Render");
  551. }
  552.  
  553. global proc layerEditorLayerButtonSelect(
  554.     int    $modifiers,
  555.     string $layerButton)
  556. //
  557. //    Description:
  558. //        This procedure is called whenever the selects a layer in
  559. //        the editor.
  560. //
  561. //    Arguments:
  562. //        $modifiers   - The state of the mouse modifiers.
  563. //
  564. //        $layerButton - The name of the layer selected.
  565. //
  566. {
  567.     global string $gCurrentLayerEditor;
  568.     global int    $gLayerEditorLastButtonSelection;
  569.  
  570.     string $currentSelection[], $remove[], $orderArray[];
  571.     string $item, $gridLayout;
  572.     int    $select, $count = 0, $index, $startIndex, $endIndex;
  573.     int    $selectionPosition;
  574.  
  575.     //    Get the type of layers currently displayed in the editor.
  576.     //
  577.     $type = getLayerDisplayType();
  578.  
  579.     //    Set the parent to the current layer editor. We can now use short
  580.     //    names of our controls and be certain they reference the correct
  581.     //    control.
  582.     //
  583.     setParent $gCurrentLayerEditor;
  584.  
  585.     //    Get the current layer selection.
  586.     //
  587.     $currentSelection = getLayerSelection($type);
  588.  
  589.     //    Get the layout containing the layer buttons.
  590.     //
  591.     if ("Display" == $type) {
  592.         $gridLayout = "LayerEditorDisplayLayerLayout";
  593.     } else {
  594.         $gridLayout = "LayerEditorRenderLayerLayout";
  595.     }
  596.  
  597.     if (0 == $modifiers) {
  598.         //
  599.         //    No modifiers. Select the button and deselect any currently
  600.         //    selected buttons. Also make the selected button the current
  601.         //    layer.
  602.         //
  603.         if ("Display" == $type) {
  604.             if (`layerButton -exists $layerButton`) {
  605.                 if (`objExists $layerButton`) {
  606.                     if (catch(`editDisplayLayerGlobals -currentDisplayLayer $layerButton`)) {
  607.                         //
  608.                         //    If there was an error then that means that the layer belongs
  609.                         //    to a layer in a referenced scene. Don't select the layer.
  610.                         //
  611.                         return;
  612.                     }
  613.                 }
  614.                 layerButton -edit -select true -current true $layerButton;
  615.             }
  616.  
  617.         } else if ("Render" == $type) {
  618.             if (`renderLayerButton -exists $layerButton`) {
  619.                 if (`objExists $layerButton`) {
  620.                     if (catch(`editRenderLayerGlobals -currentRenderLayer $layerButton`)) {
  621.                         //
  622.                         //    If there was an error then that means that the layer belongs
  623.                         //    to a layer in a referenced scene. Don't select the layer.
  624.                         //
  625.                         return;
  626.                     }
  627.                 }
  628.                 renderLayerButton -edit -select true -current true $layerButton;
  629.             }
  630.         }
  631.  
  632.         //    Get the order array of layer buttons in the layout.
  633.         //
  634.         $orderArray = `gridLayout -query -gridOrder $gridLayout`;
  635.  
  636.         //    If the selected button is already selected then remove
  637.         //    it from the list of buttons to deselect.
  638.         //
  639.         $remove[0] = $layerButton;
  640.         $currentSelection = AWRemoveStringsFromStringArray(
  641.             $remove, $currentSelection);
  642.  
  643.         //    Deselect previously selected buttons.
  644.         //
  645.         for ($item in $currentSelection) {
  646.             if ("Display" == $type) {
  647.                 layerButton -edit -select false $item;
  648.             } else if ("Render" == $type) {
  649.                 renderLayerButton -edit -select false $item;
  650.             }
  651.         }
  652.  
  653.         //    Make sure the previous current layer (if any) is reset.
  654.         //
  655.         for ($item in $orderArray) {
  656.             if ("(null)" == $item) continue;
  657.             if ($item != $layerButton) {
  658.  
  659.                 if ("Display" == $type) {
  660.                     if (`layerButton -query -current $item`) {
  661.                         layerButton -edit -current false $item;
  662.                     }
  663.  
  664.                 } else if ("Render" == $type) {
  665.                     if (`renderLayerButton -query -current $item`) {
  666.                         renderLayerButton -edit -current false $item;
  667.                     }
  668.                 }
  669.             }
  670.         }
  671.  
  672.         //    Determine and save the position of the selected button.
  673.         //    Need the position of this button for range selecting with
  674.         //    the Shift modifier.
  675.         //
  676.         for ($index = 0; $index < size($orderArray); $index++) {
  677.             if ($layerButton == $orderArray[$index]) {
  678.                 $gLayerEditorLastButtonSelection = $index;
  679.                 break;
  680.             }
  681.         }
  682.  
  683.     } else if (1 == $modifiers) {
  684.         //
  685.         //    Shift modifier. Select the range of buttons from the button
  686.         //    that was just pressed to the last button pressed.
  687.         //
  688.  
  689.         //    Determine the position of the selected button.
  690.         //
  691.         $orderArray = `gridLayout -query -gridOrder $gridLayout`;
  692.         for ($index = 0; $index < size($orderArray); $index++) {
  693.             if ($layerButton == $orderArray[$index]) {
  694.                 $selectionPosition = $index;
  695.                 break;
  696.             }
  697.         }
  698.  
  699.         $startIndex = $endIndex = 0;
  700.  
  701.         if ($selectionPosition <= $gLayerEditorLastButtonSelection) {
  702.             $startIndex = $selectionPosition;
  703.             $endIndex = $gLayerEditorLastButtonSelection;
  704.  
  705.         } else if ($selectionPosition > $gLayerEditorLastButtonSelection) {
  706.             $startIndex = $gLayerEditorLastButtonSelection;
  707.             $endIndex = $selectionPosition;
  708.         }
  709.  
  710.         for ($index = 0; $index < size($orderArray); $index++) {
  711.             if ("(null)" == $orderArray[$index]) continue;
  712.             $select = false;
  713.             if ($index >= $startIndex && $index <= $endIndex) $select = true;
  714.             if ("Display" == $type) {
  715.                 layerButton -edit -select $select $orderArray[$index];
  716.             } else if ("Render" == $type) {
  717.                 renderLayerButton -edit -select $select $orderArray[$index];
  718.             }
  719.         }
  720.  
  721.     } else if (4 == $modifiers) {
  722.         //
  723.         //    Ctrl modifier. Toggle the selection of the button.
  724.         //
  725.         if ("Display" == $type) {
  726.             $select = `layerButton -query -select $layerButton`;
  727.             layerButton -edit -select (!$select) $layerButton;
  728.         } else if ("Render" == $type) {
  729.             $select = `renderLayerButton -query -select $layerButton`;
  730.             renderLayerButton -edit -select (!$select) $layerButton;
  731.         }
  732.  
  733.         //    Is this layer also the current one? If it is then make the
  734.         //    default layer current and mark this layer as no longer being
  735.         //    current.
  736.         //
  737.         if ("Display" == $type) {
  738.             if (`layerButton -exists $layerButton`) {
  739.                 if (`layerButton -query -current $layerButton`) {
  740.                     layerButton -edit -current false $layerButton;
  741.                     editDisplayLayerGlobals -currentDisplayLayer "defaultLayer";
  742.                 }
  743.             }
  744.  
  745.         } else if ("Render" == $type) {
  746.             if (`renderLayerButton -exists $layerButton`) {
  747.                 if (`renderLayerButton -query -current $layerButton`) {
  748.                     renderLayerButton -edit -current false $layerButton;
  749.                     editRenderLayerGlobals -currentRenderLayer "defaultRenderLayer";
  750.                 }
  751.             }
  752.         }
  753.     }
  754. }
  755.  
  756. global proc layerEditorQuickEditWindowSave(string $layerArray[])
  757. //
  758. //    Description:
  759. //        This procedure is called when the user presses the Save button
  760. //        in the Layer Edit window.
  761. //
  762. //        Take the edit window values and apply them to the layer(s).
  763. //
  764. //    Arguments:
  765. //        $layerArray - The layer(s) being edited.
  766. //
  767. {
  768.     global string $gCurrentLayerEditor;
  769.  
  770.     if (!`window -exists LayerEditorQuickEditWindow`) return;
  771.     
  772.     string $name, $layer, $layerType, $newName;
  773.     int    $visible, $renderable, $colorIndex, $type;
  774.     int    $layerCount;
  775.  
  776.     //    Determine the layer type.
  777.     //
  778.     $layer = $layerArray[0];
  779.     $layerType = `objectType $layer`;
  780.  
  781.     $layerCount = size($layerArray);
  782.  
  783.     setParent LayerEditorQuickEditWindow;
  784.  
  785.     //    Determine the layer name from the edit window.
  786.     //
  787.     $name = `textFieldGrp -query -text NameField`;
  788.  
  789.     //    Get the layer attributes from the window according to the
  790.     //    layer type.
  791.     //
  792.     if ("displayLayer" == $layerType) {
  793.         $visible = `checkBoxGrp -query -value1 VisibleCheckBox`;
  794.         $type = `optionMenuGrp -query -select TypeOptionMenu`;
  795.         $colorIndex = `palettePort -query -setCurCell ColorPalette`;
  796.  
  797.     } else if ("renderLayer" == $layerType) {
  798.         $renderable = `checkBoxGrp -query -value1 RenderableCheckBox`;
  799.     }
  800.  
  801.     if (1 == $layerCount) {
  802.         //
  803.         //    Only one layer edited.
  804.         //
  805.         if ($name == $layer) {
  806.             //
  807.             //    User has not changed name of object. Just apply the
  808.             //    attributes.
  809.             //
  810.             if ("displayLayer" == $layerType) {
  811.                 setAttr ($layer + ".visibility") $visible;
  812.                 setAttr ($layer + ".displayType") ($type - 1);
  813.                 setAttr ($layer + ".color") $colorIndex;
  814.  
  815.             } else if ("renderLayer" == $layerType) {
  816.                 setAttr ($layer + ".renderable") $renderable;
  817.             }
  818.  
  819.         } else {
  820.             //
  821.             //    User has changed the name value.
  822.             //
  823.             //    Check for empty name or invalid name.
  824.             //
  825.             if ("" == $name || !isValidName($name)) {
  826.                 confirmDialog
  827.                     -title "Warning"
  828.                     -parent LayerEditorQuickEditWindow
  829.                     -button        "OK"
  830.                     -defaultButton "OK"
  831.                     -cancelButton  "OK"
  832.                     -message ("The name \"" + $name + "\" is invalid. Valid names "
  833.                         + "contain alphanumeric\ncharacters only. Whitespace "
  834.                         + "characters are not allowed.");
  835.                 return;
  836.  
  837.             } else {
  838.                 //
  839.                 //    Name change. Must check if an object by that name already
  840.                 //    exists.
  841.                 //
  842.                 if (`objExists $name`) {
  843.                     confirmDialog
  844.                         -title "Warning"
  845.                         -parent LayerEditorQuickEditWindow
  846.                         -button        "OK"
  847.                         -defaultButton "OK"
  848.                         -cancelButton  "OK"
  849.                         -message ("An object by the name \"" + $name 
  850.                             + "\" already exists.\nEnter a different name.");
  851.                     return;
  852.  
  853.                 } else {
  854.                     //
  855.                     //    Apply the attribute values.
  856.                     //
  857.                     if ("displayLayer" == $layerType) {
  858.                         setAttr ($layer + ".visibility") $visible;
  859.                         setAttr ($layer + ".displayType") ($type - 1);
  860.                         setAttr ($layer + ".color") $colorIndex;
  861.  
  862.                     } else if ("renderLayer" == $layerType) {
  863.                         setAttr ($layer + ".renderable") $renderable;
  864.                     }
  865.  
  866.                     //    Rename the layer object.
  867.                     //
  868.                     //    The renaming of the corresponding layer button will
  869.                     //    happen in the proc assigned with the button's
  870.                     //    -rc/renameCommand flag.
  871.                     //
  872.                     rename $layer $name;
  873.                 }
  874.             }
  875.         }
  876.  
  877.     } else {
  878.         //
  879.         //    More than one layer edited.
  880.         //
  881.         for ($layer in $layerArray) {
  882.             if ("displayLayer" == $layerType) {
  883.                 setAttr ($layer + ".visibility") $visible;
  884.                 setAttr ($layer + ".displayType") ($type - 1);
  885.                 setAttr ($layer + ".color") $colorIndex;
  886.  
  887.             } else if ("renderLayer" == $layerType) {
  888.                 setAttr ($layer + ".renderable") $renderable;
  889.             }
  890.         }
  891.     }
  892.  
  893.     deleteUI -window LayerEditorQuickEditWindow;
  894. }
  895.  
  896. global proc createLayerEditorQuickEditWindow(string $layerArray[])
  897. //
  898. //    Description:
  899. //        Create a quick edit window for the user to modify the layer
  900. //        attributes.
  901. //
  902. //    Arguments:
  903. //        $layerArray - The layer(s) to edit.
  904. //
  905. {
  906.     string $window, $form, $body, $buttonForm;
  907.     string $name, $nameField, $visibleCheckBox, $typeOptionMenu;
  908.     string $colorLabel, $colorPalette, $saveButton, $cancelButton;
  909.     string $renderableCheckBox, $layer, $layerType;
  910.     string $windowTitle;
  911.     int    $visible, $displayType, $colorIndex, $renderable, $index;
  912.     int    $layerCount;
  913.  
  914.     //    Make sure an argument has been supplied.
  915.     //
  916.     $layerCount = size($layerArray);
  917.     if (1 > $layerCount) {
  918.         error ("Must supply a layer argument");
  919.         return;
  920.     }
  921.  
  922.     $layer = $layerArray[0];
  923.  
  924.     //    Get the layer type.
  925.     //
  926.     $layerType = `objectType $layer`;
  927.  
  928.     //    Delete the window if it already exists.
  929.     //
  930.     if (`window -exists LayerEditorQuickEditWindow`) {
  931.         deleteUI -window LayerEditorQuickEditWindow;
  932.     }
  933.  
  934.     //    Create the window and the top level layout.
  935.     //
  936.     if (1 < $layerCount) $windowTitle = "Edit Layers";
  937.     else $windowTitle = "Edit Layer";
  938.     $window = `window -title $windowTitle -iconName $windowTitle LayerEditorQuickEditWindow`;
  939.     $form = `formLayout`;
  940.     
  941.     //    Activate the default UI template for consistent alignment.
  942.     //
  943.     setUITemplate -pushTemplate OptionsTemplate;
  944.  
  945.     //    Get the name of the first layer. If we are editing more than one 
  946.     //    layer then construct a string that contains all the layer names. 
  947.     //    It should look something like:
  948.     //
  949.     //    layer1, layer2, layer3...
  950.     //
  951.     //    Append ellispes if there are too many layers.
  952.     //
  953.     int $kNameDisplayLimit = 3;
  954.     $name = $layer;
  955.     if (1 < $layerCount) {
  956.         for ($index = 1; ($index < $layerCount && $index < $kNameDisplayLimit); $index++) {
  957.             $name += ", ";
  958.             $name += $layerArray[$index];
  959.         }
  960.         if ($layerCount > $kNameDisplayLimit) {
  961.             $name += "...";
  962.         }
  963.     }
  964.  
  965.     if ("displayLayer" == $layerType) {
  966.         $visible = `getAttr ($layer + ".visibility")`;
  967.         $displayType = `getAttr ($layer + ".displayType")`;
  968.         $colorIndex = `getAttr ($layer + ".color")`;
  969.  
  970.     } else if ("renderLayer" == $layerType) {
  971.         $renderable = `getAttr ($layer + ".renderable")`;
  972.     }
  973.  
  974.     // ----------------------------------------------------------------------
  975.     //
  976.     //    Body.
  977.     //
  978.     setParent $form;
  979.     $body = `columnLayout -rowSpacing 4`;
  980.  
  981.     $nameField = `textFieldGrp -label "Name" -text $name
  982.         -editable (1 == $layerCount) NameField`;
  983.  
  984.     if ("displayLayer" == $layerType) {
  985.         $typeOptionMenu = `optionMenuGrp -label "Display Type" TypeOptionMenu`;
  986.         menuItem -label "Normal";
  987.         menuItem -label "Template";
  988.         menuItem -label "Reference";
  989.         optionMenuGrp -edit -select ($displayType + 1) $typeOptionMenu;
  990.  
  991.         $visibleCheckBox = `checkBoxGrp -numberOfCheckBoxes 1
  992.             -label "" -label1 "Visible"
  993.             -value1 $visible
  994.             VisibleCheckBox`;
  995.  
  996.         rowLayout -numberOfColumns 2 -columnAttach 2 "left" 0;
  997.  
  998.         $colorLabel = `text -label "Color" -align "right"`;
  999.         frameLayout -labelVisible false -borderStyle "in"
  1000.             -collapsable false -collapse false;
  1001.  
  1002.         int $cellHeight = 17, $cellWidth = 17;
  1003.         int $rows = 2, $columns = 16;
  1004.  
  1005.         $colorPalette = `palettePort 
  1006.             -dimensions    $columns $rows
  1007.             -width         ($columns * $cellWidth)
  1008.             -height        ($rows * $cellHeight)
  1009.             -transparent   0
  1010.             -topDown       true
  1011.             -colorEditable false
  1012.             -setCurCell    $colorIndex
  1013.             ColorPalette`;
  1014.  
  1015.         //    Fill in the colour palette with the color's mapped to the
  1016.         //    colour indecies.
  1017.         //
  1018.         float $colorComponentArray[];
  1019.         for ($index = 1; $index <= 31; $index++) {
  1020.             $colorComponentArray = `colorIndex -query $index`;
  1021.             palettePort -edit -rgbValue $index
  1022.                 $colorComponentArray[0]
  1023.                 $colorComponentArray[1]
  1024.                 $colorComponentArray[2]
  1025.                 $colorPalette;
  1026.         }
  1027.  
  1028.         //    Set the colour of the "transparent" cell to be a light grey.
  1029.         //    Note that the "transparent" cell really indicates "no colour
  1030.         //    assignment".
  1031.         //
  1032.         palettePort -edit -rgbValue 0 0.60 0.60 0.60 $colorPalette;
  1033.  
  1034.     } else if ("renderLayer" == $layerType) {
  1035.         $renderableCheckBox = `checkBoxGrp -numberOfCheckBoxes 1
  1036.             -label "" -label1 "Renderable"
  1037.             -value1 $renderable
  1038.             RenderableCheckBox`;
  1039.     }
  1040.  
  1041.     // ----------------------------------------------------------------------
  1042.     //
  1043.     //    Buttons.
  1044.     //
  1045.     setParent $form;
  1046.     $buttonForm = `formLayout -parent $form`;
  1047.  
  1048.     //    Save button.
  1049.     //
  1050.     string $saveCommandStr = "layerEditorQuickEditWindowSave { ";
  1051.     for ($index = 0; $index < $layerCount; $index++) {
  1052.         $saveCommandStr += ("\"" + $layerArray[$index] + "\"");
  1053.         if ($index < ($layerCount - 1)) {
  1054.             $saveCommandStr += ", ";
  1055.         }
  1056.     }
  1057.     $saveCommandStr += " }";
  1058.  
  1059.     $saveButton = `button -label "Save" -command ($saveCommandStr)`;
  1060.  
  1061.     //    Cancel button.
  1062.     //
  1063.     $cancelButton = `button -label "Cancel"
  1064.         -command ("deleteUI -window " + $window)`;
  1065.  
  1066.     formLayout -edit
  1067.         -attachForm     $saveButton   "top"    0
  1068.         -attachForm     $saveButton   "left"   0
  1069.         -attachForm     $saveButton   "bottom" 0
  1070.         -attachPosition $saveButton   "right"  2 50
  1071.  
  1072.         -attachForm     $cancelButton "top"    0
  1073.         -attachPosition $cancelButton "left"   2 50
  1074.         -attachForm     $cancelButton "bottom" 0
  1075.         -attachForm     $cancelButton "right"  0
  1076.         $buttonForm;
  1077.  
  1078.     formLayout -edit
  1079.         -attachForm     $body         "top"    4
  1080.         -attachForm     $body         "left"   4
  1081.         -attachControl  $body         "bottom" 4 $buttonForm
  1082.         -attachForm     $body         "right"  4
  1083.  
  1084.         -attachNone     $buttonForm   "top"
  1085.         -attachForm     $buttonForm   "left"   4
  1086.         -attachForm     $buttonForm   "bottom" 4
  1087.         -attachForm     $buttonForm   "right"  4
  1088.         $form;
  1089.  
  1090.     //    Deactivate the default UI template.
  1091.     //
  1092.     setUITemplate -popTemplate;
  1093.  
  1094.     showWindow $window;
  1095. }
  1096.  
  1097. global proc layerEditorLayerButtonRename(
  1098.     string $oldName,
  1099.     string $newName)
  1100. //
  1101. //    Description:
  1102. //        This procedure is called whenever a layer node is renamed.
  1103. //        It is necessary to also rename the corresponding layer button.
  1104. //
  1105. //    Arguments:
  1106. //        oldName - The old layer name.
  1107. //
  1108. //        newName - The new layer name.
  1109. //
  1110. {
  1111.     global string $gCurrentLayerEditor;
  1112.  
  1113.     setParent $gCurrentLayerEditor;
  1114.  
  1115.     renameUI $oldName $newName;
  1116. }
  1117.  
  1118. global proc layerEditorLayerButtonVisibilityChange(string $layer)
  1119. //
  1120. //    Description:
  1121. //        This procedure is called whenever the user presses the visibility
  1122. //        indicator in a display layer button.
  1123. //
  1124. //        Toggle the visibility of the layer.
  1125. //
  1126. //    Arguments:
  1127. //        $layer - The layer.
  1128. //
  1129. {
  1130.     int $visible = `getAttr ($layer + ".visibility")`;
  1131.     
  1132.     if (0 == $visible) $visible = true;
  1133.     else $visible = false;
  1134.         
  1135.     setAttr ($layer + ".visibility") $visible;
  1136. }
  1137.  
  1138. global proc layerEditorLayerButtonTypeChange(string $layer)
  1139. //
  1140. //    Description:
  1141. //        This procedure is called whenever the user presses the type
  1142. //        indicator in a display layer button.
  1143. //
  1144. //        Cycle the type of the layer.
  1145. //
  1146. //        Current layer types are: Normal, Reference, and Template.
  1147. //
  1148. //    Arguments:
  1149. //        $layer - The layer.
  1150. //
  1151. {
  1152.     int $type = `getAttr ($layer + ".displayType")`;
  1153.     
  1154.     if (0 == $type) $type = 1;
  1155.     else if (1 == $type) $type = 2;
  1156.     else $type = 0;
  1157.         
  1158.     setAttr ($layer + ".displayType") $type;
  1159. }
  1160.  
  1161. global proc layerEditorLayerButtonRenderabilityChange(string $layer)
  1162. //
  1163. //    Description:
  1164. //        This procedure is called whenever the user presses the renderable
  1165. //        indicator in a render layer button.
  1166. //
  1167. //        Toggle the renderability of the layer.
  1168. //
  1169. //    Arguments:
  1170. //        $layer - The layer.
  1171. //
  1172. {
  1173.     int $renderable = `getAttr ($layer + ".renderable")`;
  1174.     
  1175.     if (0 == $renderable) $renderable = true;
  1176.     else $renderable = false;
  1177.         
  1178.     setAttr ($layer + ".renderable") $renderable;
  1179. }
  1180.  
  1181. global proc string [] layerEditorLayerButtonDrag(
  1182.     string $dragControl,
  1183.     int    $x, 
  1184.     int    $y, 
  1185.     int    $mods)
  1186. //
  1187. //    Description:
  1188. //        This procedure is called whenever the user begins a drag from
  1189. //        a layer button.
  1190. //
  1191. //        Set up a drag message so we can detect it on the drop callback.
  1192. //
  1193. //    Arguments:
  1194. //        $dragControl - The layer button where the drag occurred.
  1195. //
  1196. //        $x, $y       - Location of the drag event.
  1197. //
  1198. //        $mods        - State of the mouse modifiers.
  1199. //
  1200. //    Returns:
  1201. //        A string array value representing the type of drag message.
  1202. //
  1203. {
  1204.     string $result[] = {"LayerButtonDragMessage"};
  1205.     return $result;
  1206. }
  1207.  
  1208. global proc layerEditorLayerButtonDrop(
  1209.     string $dragControl,
  1210.     string $dropControl, 
  1211.     string $messages[],
  1212.     int    $x, 
  1213.     int    $y, 
  1214.     int    $dragType)
  1215. //
  1216. //    Description:
  1217. //        This procedure is called whenever the user ends a drag from
  1218. //        a layer button to another layer button.
  1219. //
  1220. //        If the drag message is the correct type then reorder the
  1221. //        layer buttons.
  1222. //
  1223. //    Arguments:
  1224. //        $dragControl - The layer button where the drag occurred.
  1225. //
  1226. //        $dropControl - The layer button where the drop occurred.
  1227. //
  1228. //        $messages[]  - The drag messages corresponding to the event.
  1229. //
  1230. //        $x, $y       - Location of the drag event.
  1231. //
  1232. //        $dragType    - Type of drag event.
  1233. //
  1234. {
  1235.     global string $gCurrentLayerEditor;
  1236.  
  1237.     string $type, $gridLayout = "", $orderArray[];
  1238.     string $tokenArray[], $shortName = "";
  1239.     int    $index, $tokenCount, $dragPosition = 0, $dropPosition = 0;
  1240.  
  1241.     //    Get the type of layers currently displayed in the editor.
  1242.     //
  1243.     $type = getLayerDisplayType();
  1244.  
  1245.     //    Set the parent to the current layer editor. We can now use short
  1246.     //    names of our controls and be certain they reference the correct
  1247.     //    control.
  1248.     //
  1249.     setParent $gCurrentLayerEditor;
  1250.  
  1251.     if ("Display" == $type) {
  1252.         $gridLayout = "LayerEditorDisplayLayerLayout";
  1253.  
  1254.     } else if ("Render" == $type) {
  1255.         $gridLayout = "LayerEditorRenderLayerLayout";
  1256.     }
  1257.  
  1258.     if ("LayerButtonDragMessage" == $messages[0]) {
  1259.  
  1260.         //    Note that the drag and drop control arguments are long names and
  1261.         //    the items in the grid's array are short names (long names 
  1262.         //    contain the full path).
  1263.         //
  1264.         $dropControl = getShortName($dropControl);
  1265.         $dragControl = getShortName($dragControl);
  1266.  
  1267.         $orderArray = `gridLayout -query -gridOrder $gridLayout`;
  1268.  
  1269.         //    Determine the drag and drop positions.
  1270.         //
  1271.         for ($index = 0; $index < size($orderArray); $index++) {
  1272.             if ($dragControl == $orderArray[$index]) {
  1273.                 $dragPosition = $index + 1;
  1274.             }
  1275.             if ($dropControl == $orderArray[$index]) {
  1276.                 $dropPosition = $index + 1;
  1277.             }
  1278.         }
  1279.  
  1280.         if ($dragPosition < $dropPosition) {
  1281.             
  1282.             //    Put drag control at drop position.
  1283.             //
  1284.             gridLayout -edit -position $dragControl $dropPosition $gridLayout;
  1285.  
  1286.             //    Shuffle the items following the drop position.
  1287.             //
  1288.             for ($index = $dragPosition + 1; $index <= $dropPosition; $index++) {
  1289.                 gridLayout -edit -position $orderArray[$index - 1] ($index - 1) $gridLayout;
  1290.             }
  1291.  
  1292.         } else if ($dragPosition > $dropPosition) {
  1293.         
  1294.             //    Put drag control at drop position.
  1295.             //
  1296.             gridLayout -edit -position $dragControl $dropPosition $gridLayout;
  1297.  
  1298.             //    Shuffle the items following the drop position.
  1299.             //
  1300.             for ($index = $dragPosition - 1; $index >= $dropPosition; $index--) {
  1301.                 gridLayout -edit -position $orderArray[$index - 1] ($index + 1) $gridLayout;
  1302.             }
  1303.  
  1304.         } else {
  1305.             //
  1306.             //    Drag position is same as drop position. Nothing to do.
  1307.             //
  1308.         }
  1309.     }
  1310.     
  1311.     updateLayerOrderByType($type);
  1312. }
  1313.  
  1314. global proc layerEditorShowEditMenu(string $menu)
  1315. //
  1316. //    Description:
  1317. //        This procedure is called whenever the user shows the Edit menu.
  1318. //
  1319. //        Update the enable state of the menu items to reflect the possible
  1320. //        options.
  1321. //
  1322. //    Arguments:
  1323. //        $menu - The menu.
  1324. //
  1325. {
  1326.     string $selection[] = `ls -selection`;
  1327.     string $layerSelection[] = getLayerSelection("");
  1328.     string $layerContents[], $remove[];
  1329.     string $currentLayer = getCurrentLayer("");
  1330.     string $type;
  1331.     int    $enable = false;
  1332.     
  1333.     $type = getLayerDisplayType();
  1334.  
  1335.     //    Remove layers from the selection list.
  1336.     //
  1337.     $remove = `ls -type displayLayer`;
  1338.     $selection = AWRemoveStringsFromStringArray($remove, $selection);
  1339.     $remove = `ls -type renderLayer`;
  1340.     $selection = AWRemoveStringsFromStringArray($remove, $selection);
  1341.     
  1342.     setParent -menu $menu;
  1343.  
  1344.     //    Delete layer. Enable if there is at least one layer selected.
  1345.     //
  1346.     $enable = false;
  1347.     if (0 < size($layerSelection)) $enable = true;
  1348.     menuItem -edit -enable $enable LayerEditorEditMenuDeleteMenuItem;
  1349.  
  1350.     //    Edit layer. Enable if there is a current layer.
  1351.     //
  1352.     $enable = false;
  1353.     if ("" != $currentLayer) $enable = true;
  1354.     menuItem -edit -enable $enable LayerEditorEditMenuEditMenuItem;
  1355.  
  1356.     //    Select objects in selected layer(s). Enable if there is at least
  1357.     //    one layer selected and there is at least one object in any of 
  1358.     //    those layers.
  1359.     //
  1360.     $enable = false;
  1361.     for ($layer in $layerSelection) {
  1362.         if ("Display" == $type) {
  1363.             $layerContents = `editDisplayLayerMembers -query $layer`;
  1364.         } else if ("Render" == $type) {
  1365.             $layerContents = `editRenderLayerMembers -query $layer`;
  1366.         }
  1367.         if (0 < size($layerContents)) {
  1368.             $enable = true;
  1369.             break;
  1370.         }
  1371.     }
  1372.     menuItem -edit -enable $enable LayerEditorEditMenuSelectObjectsMenuItem;
  1373.  
  1374.     //    Add selected objects to current layer. Enable if there is a 
  1375.     //    current layer and objects on the selection list.
  1376.     //
  1377.     $enable = false;
  1378.     if ("" != $currentLayer && 0 < size($selection)) $enable = true;
  1379.     menuItem -edit -enable $enable LayerEditorEditMenuAddObjectsMenuItem;
  1380.  
  1381.     //    Remove objects from selected layer(s). Enable if there is at least
  1382.     //    one layer selected and there is at least one object in any of
  1383.     //    those layers.
  1384.     //
  1385.     $enable = false;
  1386.     for ($layer in $layerSelection) {
  1387.         if ("Display" == $type) {
  1388.             $layerContents = `editDisplayLayerMembers -query $layer`;
  1389.         } else if ("Render" == $type) {
  1390.             $layerContents = `editRenderLayerMembers -query $layer`;
  1391.         }
  1392.         if (0 < size($layerContents)) {
  1393.             $enable = true;
  1394.             break;
  1395.         }
  1396.     }
  1397.     menuItem -edit -enable $enable LayerEditorEditMenuRemoveObjectsMenuItem;
  1398.  
  1399.     //    Remove selected objects from whatever layer they might be in. In other
  1400.     //    words add the selected objects to the default layer.
  1401.     //
  1402.     //    Enable this item if there are selected objects.
  1403.     //
  1404.     $enable = false;
  1405.     if (0 < size($selection)) $enable = true;
  1406.     menuItem -edit -enable $enable LayerEditorEditMenuRemoveFromLayerMenuItem;
  1407.  
  1408.     //    Show Layer Attributes. Enable if there is a current layer.
  1409.     //
  1410.     $enable = false;
  1411.     if ("" != $currentLayer) $enable = true;
  1412.     menuItem -edit -enable $enable LayerEditorEditMenuLayerAttributesMenuItem;
  1413. }
  1414.  
  1415. global proc layerEditorShowOptionsMenu(string $menu)
  1416. //
  1417. //    Description:
  1418. //        This procedure is called whenever the user shows the Options menu.
  1419. //
  1420. //        Update the state of the menu items.
  1421. //
  1422. //    Arguments:
  1423. //        $menu - The menu.
  1424. //
  1425. {
  1426.     setParent -menu $menu;
  1427.  
  1428.     $value = `optionVar -query displayLayerCurrent`;
  1429.     menuItem -edit -checkBox $value LayerEditorOptionsMenuMakeCurrentMenuItem;
  1430.  
  1431.     $value = `editDisplayLayerGlobals -query -useCurrent`;
  1432.     menuItem -edit -checkBox $value LayerEditorOptionsMenuUseCurrentMenuItem;
  1433. }
  1434.  
  1435. global proc layerEditorShowPopupMenu(string $menu, string $layerButton)
  1436. //
  1437. //    Description:
  1438. //        This procedure is called when the popup menu on layer buttons
  1439. //        is shown.
  1440. //
  1441. //        Create the menu items.
  1442. //
  1443. //    Arguments:
  1444. //        $menu  - The parent menu.
  1445. //
  1446. //        $layer - The full path name to the layer button.
  1447. //
  1448. {
  1449.     string $selection[] = `ls -selection`;
  1450.     string $layerContents[], $type, $layer;
  1451.     int    $enable = false;
  1452.  
  1453.     //    Get the current layer type.
  1454.     //
  1455.     $type = getLayerDisplayType();
  1456.  
  1457.     //    Get the short name of the layer from the layer button argument.
  1458.     //
  1459.     $layer = getShortName($layerButton);
  1460.  
  1461.     //    Get the contents of this layer.
  1462.     //
  1463.     if ("Display" == $type) {
  1464.         $layerContents = `editDisplayLayerMembers -query $layer`;
  1465.     } else if ("Render" == $type) {
  1466.         $layerContents = `editRenderLayerMembers -query $layer`;
  1467.     }
  1468.  
  1469.     //    Remove layers from the selection list.
  1470.     //
  1471.     $remove = `ls -type displayLayer`;
  1472.     $selection = AWRemoveStringsFromStringArray($remove, $selection);
  1473.     $remove = `ls -type renderLayer`;
  1474.     $selection = AWRemoveStringsFromStringArray($remove, $selection);
  1475.     
  1476.     //    Set the menu parent and delete all the items in the menu.
  1477.     //
  1478.     setParent -menu $menu;
  1479.     menu -edit -deleteAllItems $menu;
  1480.  
  1481.     //
  1482.     //    Now create the menu items...
  1483.     //
  1484.  
  1485.     //    New layer.
  1486.     //
  1487.     menuItem -label "Create Layer" -command ("layerEditorCreateLayer");
  1488.  
  1489.     //    Delete this layer only (as opposed to deleting all the selected layers).
  1490.     //
  1491.     menuItem -label "Delete"
  1492.         -command ("evalDeferred (\"layerEditorDeleteLayer " + $layer + "\")");
  1493.  
  1494.     //    Edit layer.
  1495.     //
  1496.     menuItem -label "Edit Layer..." -command ("layerEditorEditLayer " + $layer);
  1497.  
  1498.     menuItem -divider true;
  1499.  
  1500.     //    Select objects in this layer. Enable if this layer contains
  1501.     //    an object.
  1502.     //
  1503.     $enable = false;
  1504.     if (0 < size($layerContents)) $enable = true;
  1505.     menuItem -label "Select Objects" -enable $enable
  1506.         -command ("layerEditorSelectObjects " + $layer);
  1507.  
  1508.     //    Add selected objects to this layer. Enable if there is at least
  1509.     //    one object on the selection list.
  1510.     //
  1511.     $enable = false;
  1512.     if (0 < size($selection)) $enable = true;
  1513.     menuItem -label "Add Selected Objects" -enable $enable
  1514.         -command ("layerEditorAddObjects " + $layer);
  1515.  
  1516.     //    Remove objects from this layer.  Enable if this layer contains
  1517.     //    an object.
  1518.     //
  1519.     $enable = false;
  1520.     if (0 < size($layerContents)) $enable = true;
  1521.     menuItem -label "Remove Objects" -enable $enable
  1522.         -command ("layerEditorRemoveObjects " + $layer);
  1523.  
  1524.     menuItem -divider true;
  1525.     
  1526.     menuItem -label "Attributes..."
  1527.         -command ("layerEditorLayerAttributes " + $layer);
  1528.     menuItem -label "Membership..."
  1529.         -command ("layerEditorMembership " + $layer);
  1530. }
  1531.  
  1532. global proc layerEditorCreateLayer()
  1533. //
  1534. //    Description:
  1535. //        Create a new layer of the appropriate type and update the 
  1536. //        editor to reflect the change.
  1537. //
  1538. {
  1539.     global string $gCurrentLayerEditor;
  1540.  
  1541.     string $type, $createLayerCmd = "";
  1542.     string $newLayer, $layer, $currentSelection[];
  1543.  
  1544.     //    Get the type of layers currently displayed in the editor.
  1545.     //
  1546.     $type = getLayerDisplayType();
  1547.  
  1548.     //    Set the parent to the current layer editor. We can now use short
  1549.     //    names of our controls and be certain they reference the correct
  1550.     //    control.
  1551.     //
  1552.     setParent $gCurrentLayerEditor;
  1553.  
  1554.     if ("Display" == $type) {
  1555.         $createLayerCmd = "performCreateDisplayLayer 0";
  1556.  
  1557.     } else if ("Render" == $type) {
  1558.         $createLayerCmd = "performCreateRenderLayer 0";
  1559.     }
  1560.  
  1561.     if ("" != $createLayerCmd) {
  1562.         eval ($createLayerCmd);
  1563.     }
  1564.     
  1565.     updateLayersByType($type);
  1566.  
  1567.     //    Select and make current the new layer if the option to do so is set.
  1568.     //
  1569.     if (`optionVar -query displayLayerCurrent`) {
  1570.  
  1571.         //    Deselect any selected layers.
  1572.         //
  1573.         $currentSelection = getLayerSelection($type);
  1574.         for ($layer in $currentSelection) {
  1575.             if ("Display" == $type) {
  1576.                 layerButton -edit -select false -current false $layer;
  1577.             } else if ("Render" == $type) {
  1578.                 renderLayerButton -edit -select false -current false $layer;
  1579.             }
  1580.         }
  1581.  
  1582.         //    Select and make current the new layer.
  1583.         //
  1584.         if ("Display" == $type) {
  1585.             $newLayer = `editDisplayLayerGlobals -query -currentDisplayLayer`;
  1586.             if (`layerButton -exists $newLayer`) {
  1587.                 layerButton -edit -select true -current true $newLayer;
  1588.             }
  1589.         } else if ("Render" == $type) {
  1590.             $newLayer = `editRenderLayerGlobals -query -currentRenderLayer`;
  1591.             if (`renderLayerButton -exists $newLayer`) {
  1592.                 renderLayerButton -edit -select true -current true $newLayer;
  1593.             }
  1594.         }
  1595.     }
  1596. }
  1597.  
  1598. global proc layerEditorDeleteLayer(string $layer)
  1599. //
  1600. //    Description:
  1601. //        Delete the specified layer or if no layer argument given then
  1602. //        delete all the current selected layers.
  1603. //
  1604. //    Arguments:
  1605. //        $layer - The specific layer to delete, or an empty string to
  1606. //                 delete all selected layers.
  1607. //
  1608. {
  1609.     string $type, $item, $currentSelection[];
  1610.  
  1611.     //    Get the type of layers currently displayed in the editor.
  1612.     //
  1613.     $type = getLayerDisplayType();
  1614.  
  1615.     if ("" != $layer) {
  1616.         //
  1617.         //    Delete the specified layer.
  1618.         //
  1619.         deleteUI -control $layer;
  1620.  
  1621.         //    Delete the layer object.
  1622.         //
  1623.         delete $layer;
  1624.  
  1625.     } else {
  1626.         //
  1627.         //    Delete the selected layers.
  1628.         //
  1629.  
  1630.         //    Get the selected layers.
  1631.         //
  1632.         $currentSelection = getLayerSelection("");
  1633.  
  1634.         //    Delete all the selected layers.
  1635.         //
  1636.         for ($item in $currentSelection) {
  1637.  
  1638.             //    Delete the layer button.
  1639.             //
  1640.             deleteUI -control $item;
  1641.  
  1642.             //    Delete the layer object.
  1643.             //
  1644.             delete $item;
  1645.         }
  1646.     }
  1647.  
  1648.     updateLayersByType($type);
  1649. }
  1650.  
  1651. global proc layerEditorEditLayer(string $layer)
  1652. //
  1653. //    Description:
  1654. //        Edit the specified layer or if no layer argument given then
  1655. //        edit the selected layers.
  1656. //
  1657. //    Arguments:
  1658. //        $layer - The specific layer to edit, or an empty string to
  1659. //                 edit the selected layers.
  1660. //
  1661. {
  1662.     string $layerArray[];
  1663.     
  1664.     //    Get the layer selection if the argument is an empty string.
  1665.     //
  1666.     if ("" == $layer) {
  1667.         $layerArray = getLayerSelection("");
  1668.  
  1669.     } else {
  1670.         $layerArray[0] = $layer;
  1671.     }
  1672.  
  1673.     //    Show the layer edit window.
  1674.     //
  1675.     createLayerEditorQuickEditWindow($layerArray);
  1676. }
  1677.  
  1678. global proc layerEditorSelectObjects(string $layer)
  1679. //
  1680. //    Description:
  1681. //        Select the objects contained in the specified layer or if no 
  1682. //        layer argument given then select all the objects in the selected
  1683. //        layers.
  1684. //
  1685. //    Arguments:
  1686. //        $layer - The specific layer to select the objects from, or an empty 
  1687. //                 string to select all the objects from all selected layers.
  1688. //
  1689. {
  1690.     string $type, $item, $currentSelection[];
  1691.  
  1692.     //    Clear the current object selection list.
  1693.     //
  1694.     select -clear;
  1695.  
  1696.     //    Get the type of layers currently displayed in the editor.
  1697.     //
  1698.     $type = getLayerDisplayType();
  1699.  
  1700.     if ("" != $layer) {
  1701.         //
  1702.         //    Select the objects in the given layer.
  1703.         //
  1704.         if ("Display" == $type) {
  1705.             select -add `editDisplayLayerMembers -fullNames -query $layer`;
  1706.         } else if ("Render" == $type) {
  1707.             select -add `editRenderLayerMembers -fullNames -query $layer`;
  1708.         }
  1709.  
  1710.     } else {
  1711.         //
  1712.         //    Get the selected layers.
  1713.         //
  1714.         $currentSelection = getLayerSelection($type);
  1715.  
  1716.         //    Add to the selection list the objects contained in each 
  1717.         //    selected layer.
  1718.         //
  1719.         for ($item in $currentSelection) {
  1720.             if ("Display" == $type) {
  1721.                 select -add `editDisplayLayerMembers -fullNames -query $item`;
  1722.             } else if ("Render" == $type) {
  1723.                 select -add `editRenderLayerMembers -fullNames -query $item`;
  1724.             }
  1725.         }
  1726.     }
  1727. }
  1728.  
  1729. global proc layerEditorAddObjects(string $layer)
  1730. //
  1731. //    Description:
  1732. //        Add the objects on the selection list to the specified layer or if
  1733. //        no layer argument given then add the objects to the current layer.
  1734. //
  1735. //    Arguments:
  1736. //        $layer - The specific layer to add the objects to, or an empty 
  1737. //                 string to add the objects to the current layer.
  1738. //
  1739. {
  1740.     //    Get the current layer if the argument is an empty string.
  1741.     //
  1742.     if ("" == $layer) $layer = getCurrentLayer("");
  1743.  
  1744.     //    Get the type of layers currently displayed in the editor.
  1745.     //
  1746.     $type = getLayerDisplayType();
  1747.  
  1748.     //    Add the objects to the layer.
  1749.     //
  1750.     if ("" != $layer) {
  1751.         if (`optionVar -query displayLayerContents` == 2) {
  1752.             if ("Display" == $type) {
  1753.                 editDisplayLayerMembers -noRecurse $layer `ls -selection`;
  1754.             } else if ("Render" == $type) {
  1755.                 editRenderLayerMembers -noRecurse $layer `ls -selection`;
  1756.             }
  1757.  
  1758.         } else {
  1759.             if ("Display" == $type) {
  1760.                 editDisplayLayerMembers $layer `ls -selection`;
  1761.             } else if ("Render" == $type) {
  1762.                 editRenderLayerMembers $layer `ls -selection`;
  1763.             }
  1764.         }
  1765.     }
  1766. }
  1767.  
  1768. global proc layerEditorRemoveObjects(string $layer)
  1769. //
  1770. //    Description:
  1771. //        Remove all the objects contained in the specified layer or if no 
  1772. //        layer argument given then remove all the objects in the selected
  1773. //        layers.
  1774. //
  1775. //    Arguments:
  1776. //        $layer - The specific layer to remove the objects from, or an empty 
  1777. //                 string to remove all the objects from all selected layers.
  1778. //
  1779. {
  1780.     string $type, $item, $currentSelection[];
  1781.  
  1782.     //    Get the type of layers currently displayed in the editor.
  1783.     //
  1784.     $type = getLayerDisplayType();
  1785.  
  1786.     if ("" != $layer) {
  1787.         //
  1788.         //    Add to the default layer all the objects in the given layer.
  1789.         //
  1790.         if ("Display" == $type) {
  1791.             editDisplayLayerMembers "defaultLayer"
  1792.                 `editDisplayLayerMembers -fullNames -query $layer`;
  1793.         } else if ("Render" == $type) {
  1794.             editRenderLayerMembers "defaultRenderLayer"
  1795.                 `editRenderLayerMembers -fullNames -query $layer`;
  1796.         }
  1797.  
  1798.     } else {
  1799.         //
  1800.         //    Get the selected layers.
  1801.         //
  1802.         $currentSelection = getLayerSelection($type);
  1803.  
  1804.         //    Add to the default layer all the objects in the selected
  1805.         //    layer(s).
  1806.         //
  1807.         for ($item in $currentSelection) {
  1808.             if ("Display" == $type) {
  1809.                 editDisplayLayerMembers "defaultLayer"
  1810.                     `editDisplayLayerMembers -fullNames -query $item`;
  1811.             } else if ("Render" == $type) {
  1812.                 editRenderLayerMembers "defaultRenderLayer"
  1813.                     `editRenderLayerMembers -fullNames -query $item`;
  1814.             }
  1815.         }
  1816.     }
  1817. }
  1818.  
  1819. global proc layerEditorSelectUnused()
  1820. //
  1821. //    Description:
  1822. //        Select all the layers that don't have any objects in them.
  1823. //
  1824. {
  1825.     global string $gCurrentLayerEditor;
  1826.  
  1827.     string $type, $objectType, $layer, $layout;
  1828.     string $layerArray[], $layerContents[];
  1829.     string $selectArray[], $currentSelection[];
  1830.     int    $index;
  1831.  
  1832.     //    Get the type of layers currently displayed in the editor.
  1833.     //
  1834.     $type = getLayerDisplayType();
  1835.  
  1836.     //    Set the parent to the current layer editor. We can now use short
  1837.     //    names of our controls and be certain they reference the correct
  1838.     //    control.
  1839.     //
  1840.     setParent $gCurrentLayerEditor;
  1841.  
  1842.     //    Get the current layer selection.
  1843.     //
  1844.     $currentSelection = getLayerSelection($type);
  1845.  
  1846.     //    Determine the object type of layers being displayed.
  1847.     //
  1848.     if ("Display" == $type) {
  1849.         $objectType = "displayLayer";
  1850.     } else {
  1851.         $objectType = "renderLayer";
  1852.     }
  1853.  
  1854.     //    Get all layers of the appropriate type.
  1855.     //
  1856.     $layerArray = `ls -type $objectType`;
  1857.  
  1858.     //    Determine the contents of each layer. Ignore default layers.
  1859.     //
  1860.     $index = 0;
  1861.     for ($layer in $layerArray) {
  1862.  
  1863.         //    Ignore default layers. Default layers have an id of 0.
  1864.         //
  1865.         if (0 < `getAttr ($layer + ".identification")`) {
  1866.             if ("Display" == $type) {
  1867.                 $layerContents = `editDisplayLayerMembers -query $layer`;
  1868.             } else if ("Render" == $type) {
  1869.                 $layerContents = `editRenderLayerMembers -query $layer`;
  1870.             }
  1871.             if (0 == size($layerContents)) {
  1872.                 //
  1873.                 //    This layer has nothing in it. Add it to array.
  1874.                 //
  1875.                 $selectArray[$index++] = $layer;
  1876.             }
  1877.         }
  1878.     }
  1879.  
  1880.     //
  1881.     //    Deselect any selected layers.
  1882.     //
  1883.     for ($layer in $currentSelection) {
  1884.         if ("Display" == $type) {
  1885.             layerButton -edit -select false -current false $layer;
  1886.         } else if ("Render" == $type) {
  1887.             renderLayerButton -edit -select false -current false $layer;
  1888.         }
  1889.     }
  1890.  
  1891.     //    Select all the unused layers.
  1892.     //
  1893.     for ($layer in $selectArray) {
  1894.         if ("Display" == $type) {
  1895.             layerButton -edit -select true $layer;
  1896.         } else if ("Render" == $type) {
  1897.             renderLayerButton -edit -select true $layer;
  1898.         }
  1899.     }
  1900. }
  1901.  
  1902. global proc layerEditorRemoveFromLayer()
  1903. //
  1904. //    Description:
  1905. //        Remove the selected objects from whatever layer they might be in.
  1906. //
  1907. {
  1908.     string $type, $object, $selection[], $remove[];
  1909.  
  1910.     $type = getLayerDisplayType();
  1911.  
  1912.     //    Get the selection list.
  1913.     //
  1914.     $selection = `ls -selection`;
  1915.  
  1916.     //    Remove layers from the selection list.
  1917.     //
  1918.     $remove = `ls -type displayLayer`;
  1919.     $selection = AWRemoveStringsFromStringArray($remove, $selection);
  1920.     $remove = `ls -type renderLayer`;
  1921.     $selection = AWRemoveStringsFromStringArray($remove, $selection);
  1922.  
  1923.     for ($object in $selection) {
  1924.         if ("Display" == $type) {
  1925.             editDisplayLayerMembers "defaultLayer" $object;
  1926.         } else if ("Render" == $type) {
  1927.             editRenderLayerMembers "defaultRenderLayer" $object;
  1928.         }
  1929.     }
  1930. }
  1931.  
  1932. global proc layerEditorLayerAttributes(string $layer)
  1933. //
  1934. //    Description:
  1935. //        Show the Attribute Editor for the specified layer or if
  1936. //        no layer argument given then use the current layer.
  1937. //
  1938. //    Arguments:
  1939. //        $layer - The specific layer to show the Attribute Editor for,
  1940. //                 or an empty string to show the editor for the current layer.
  1941. //
  1942. {
  1943.     //    Get the current layer if the argument is an empty string.
  1944.     //
  1945.     if ("" == $layer) $layer = getCurrentLayer("");
  1946.  
  1947.     //    Get the current layer.
  1948.     //
  1949.     if ("" != $layer) {
  1950.  
  1951.         //    Actually select the layer object.
  1952.         //
  1953.         select -replace $layer;
  1954.  
  1955.         //    Show the Attribute Editor.
  1956.         //
  1957.         editSelected();
  1958.     }
  1959. }
  1960.  
  1961. global proc layerEditorMembership(string $layer)
  1962. //
  1963. //    Description:
  1964. //        Show the Relationship Editor for the specified layer or if
  1965. //        no layer argument given then use the layer selection.
  1966. //
  1967. //    Arguments:
  1968. //        $layer - The specific layer to show the Relationship Editor for,
  1969. //                 or an empty string to show the editor for the selected
  1970. //                 layers.
  1971. //
  1972. {
  1973.     string $selection[];
  1974.  
  1975.     if ("" != $layer) {
  1976.         $selection[0] = $layer;
  1977.  
  1978.     } else {
  1979.         //
  1980.         //    Get the current selection.
  1981.         //
  1982.         $selection = getLayerSelection("");
  1983.     }
  1984.  
  1985.     //    Show the Relationship Editor.
  1986.     //
  1987.     relationshipEditor("displayLayers", 7, $selection, {});
  1988. }
  1989.  
  1990. global proc layerEditorDisplayTypeChange()
  1991. //
  1992. //    Description:
  1993. //        This procedure is called whenever the user changes the type
  1994. //        of layers displayed in the Layer Editor.
  1995. //
  1996. {
  1997.     global string $gCurrentLayerEditor;
  1998.  
  1999.     string $type, $tab = "";
  2000.  
  2001.     //    Get the type of layers currently displayed in the editor.
  2002.     //
  2003.     $type = getLayerDisplayType();
  2004.  
  2005.     if ("Display" == $type) {
  2006.         $tab = "DisplayLayerTab";
  2007.     } else if ("Render" == $type) {
  2008.         $tab = "RenderLayerTab";
  2009.     }
  2010.  
  2011.     setParent $gCurrentLayerEditor;
  2012.  
  2013.     if ("" != $tab) {
  2014.         tabLayout -edit -selectTab $tab DisplayLayerUITabLayout;
  2015.     }    
  2016. }
  2017.  
  2018. global proc updateLayersByType(string $type)
  2019. //
  2020. //    Description:
  2021. //        Update the Layer Editor according to the specified type.
  2022. //
  2023. //        Create and/or delete layer buttons as necessary.
  2024. //
  2025. //    Arguments:
  2026. //        $type - Type of layers to update. Values are "Display" or
  2027. //                "Render".
  2028. //
  2029. {
  2030.     global string $gCurrentLayerEditor;
  2031.  
  2032.     if ("" == $gCurrentLayerEditor) {
  2033.         error ("There is no current Layer Editor");
  2034.         return;
  2035.     }
  2036.  
  2037.     setParent $gCurrentLayerEditor;
  2038.  
  2039.     string $buttonLayout, $objectType;
  2040.     string $layer, $layerArray[];
  2041.     string $button, $buttonArray[];
  2042.     string $removeArray[];
  2043.     int    $layerID, $index, $colorIndex, $transparent;
  2044.  
  2045.     //    Width of layer buttons.
  2046.     //
  2047.     int    $kLayerButtonWidth = 350;
  2048.  
  2049.     if ("Display" == $type) {
  2050.         $buttonLayout = "LayerEditorDisplayLayerLayout";
  2051.         $objectType = "displayLayer";
  2052.  
  2053.     } else if ("Render" == $type) {
  2054.         $buttonLayout = "LayerEditorRenderLayerLayout";
  2055.         $objectType = "renderLayer";
  2056.  
  2057.     } else {
  2058.         error ("Invalid layer type: " + $type);
  2059.         return;
  2060.     }
  2061.  
  2062.     //    Get all layers of the appropriate type.
  2063.     //
  2064.     $layerArray = `ls -type $objectType`;
  2065.  
  2066.     //    Remove the default layers. Default layers have an id of 0.
  2067.     //
  2068.     $index = 0;
  2069.     for ($layer in $layerArray) {
  2070.         if (0 == `getAttr ($layer + ".identification")`) {
  2071.             $removeArray[$index++] = $layer;
  2072.         }
  2073.     }
  2074.     $layerArray = AWRemoveStringsFromStringArray($removeArray, $layerArray);
  2075.  
  2076.     //    Now sort the remaining layers according to their display order.
  2077.     //
  2078.     $layerArray = sortLayers($layerArray);
  2079.  
  2080.     //    Get the current array of layer buttons.
  2081.     //
  2082.     $buttonArray = `layout -query -childArray $buttonLayout`;
  2083.  
  2084.     //    Add buttons for new layers.
  2085.     //
  2086.     for ($layer in $layerArray) {
  2087.         if (0 == AWNumberOfOccurrencesInStringArray($layer, $buttonArray)) {
  2088.             //
  2089.             //    Create button.
  2090.             //
  2091.             if ("Display" == $type) {
  2092.                 $transparent = false;
  2093.                 $colorIndex = `getAttr ($layer + ".color")`;
  2094.                 if (0 == $colorIndex) $transparent = true;
  2095.  
  2096.                 layerButton -parent $buttonLayout
  2097.                     -label              $layer
  2098.                     -name               $layer
  2099.                     -transparent        $transparent
  2100.                     -width              $kLayerButtonWidth
  2101.                     -command            ("layerEditorLayerButtonSelect #m #1")
  2102.                     -doubleClickCommand ("createLayerEditorQuickEditWindow { \"#1\" }")
  2103.                     -renameCommand      ("layerEditorLayerButtonRename #1 #2")
  2104.                     -typeCommand        ("layerEditorLayerButtonTypeChange #1")
  2105.                     -visibleCommand     ("layerEditorLayerButtonVisibilityChange #1")
  2106.                     -dropCallback       ("layerEditorLayerButtonDrop")
  2107.                     -dragCallback       ("layerEditorLayerButtonDrag")
  2108.                     $layer;
  2109.  
  2110.             } else if ("Render" == $type) {
  2111.                 renderLayerButton -parent $buttonLayout
  2112.                     -label              $layer
  2113.                     -name               $layer
  2114.                     -width              $kLayerButtonWidth
  2115.                     -command            ("layerEditorLayerButtonSelect #m #1")
  2116.                     -doubleClickCommand ("createLayerEditorQuickEditWindow { \"#1\" }")
  2117.                     -renameCommand      ("layerEditorLayerButtonRename #1 #2")
  2118.                     -renderableCommand  ("layerEditorLayerButtonRenderabilityChange #1")
  2119.                     -dropCallback       ("layerEditorLayerButtonDrop")
  2120.                     -dragCallback       ("layerEditorLayerButtonDrag")
  2121.                     $layer;
  2122.             }
  2123.  
  2124.             //    Attach a popup menu to each layer button.
  2125.             //
  2126.             $popupMenu = `popupMenu -button 3 -parent $layer`;
  2127.             popupMenu -edit
  2128.                 -postMenuCommand ("layerEditorShowPopupMenu #1 #2")
  2129.                 $popupMenu;
  2130.  
  2131.         }
  2132.     }
  2133.  
  2134.     //    Remove buttons for deleted layers.
  2135.     //
  2136.     $buttonArray = `layout -query -childArray $buttonLayout`;
  2137.     for ($button in $buttonArray) {
  2138.         if (!`objExists $button`) {
  2139.             //
  2140.             //    Delete button.
  2141.             //
  2142.             deleteUI $button;
  2143.         }
  2144.     }
  2145.  
  2146.     if (0 < size($buttonArray) || 0 < size($layerArray)) {
  2147.         updateLayerOrderByType($type);
  2148.     }
  2149. }
  2150.  
  2151. global proc updateLayerOrderByType(string $type)
  2152. //
  2153. //    Description:
  2154. //        Update the order of layers given their current display order.
  2155. //
  2156. //        Set the layer's display order attribute based on the layer
  2157. //        button's position in the editor.
  2158. //
  2159. //    Arguments:
  2160. //        $type - Type of layer. Values are "Display" or "Render".
  2161. //
  2162. {
  2163.     global string $gCurrentLayerEditor;
  2164.  
  2165.     if ("" == $gCurrentLayerEditor) {
  2166.         error ("There is no current Layer Editor");
  2167.         return;
  2168.     }
  2169.  
  2170.     setParent $gCurrentLayerEditor;
  2171.  
  2172.     string $buttonLayout, $objectType;
  2173.     string $layer, $layerArray[];
  2174.     string $button, $buttonArray[];
  2175.     string $removeArray[];
  2176.     int    $index;
  2177.  
  2178.     if ("Display" == $type) {
  2179.         $buttonLayout = "LayerEditorDisplayLayerLayout";
  2180.         $objectType = "displayLayer";
  2181.  
  2182.     } else if ("Render" == $type) {
  2183.         $buttonLayout = "LayerEditorRenderLayerLayout";
  2184.         $objectType = "renderLayer";
  2185.  
  2186.     } else {
  2187.         error ("Invalid layer type: " + $type);
  2188.         return;
  2189.     }
  2190.  
  2191.     //    Get all the layers of the appropriate type.
  2192.     //
  2193.     $layerArray = `ls -type $objectType`;
  2194.  
  2195.     //    Remove the default layers. Default layers have an id of 0.
  2196.     //
  2197.     $index = 0;
  2198.     for ($layer in $layerArray) {
  2199.         if (0 == `getAttr ($layer + ".identification")`) {
  2200.             $removeArray[$index++] = $layer;
  2201.         }
  2202.     }
  2203.     $layerArray = AWRemoveStringsFromStringArray($removeArray, $layerArray);
  2204.  
  2205.     //    Set the display order attribute for each layer.
  2206.     //
  2207.     $buttonArray = `gridLayout -query -gridOrder $buttonLayout`;
  2208.     for ($index = 0; $index < size($buttonArray); $index++) {
  2209.         if (`objExists $buttonArray[$index]`) {
  2210.             setAttr ($buttonArray[$index] + ".displayOrder") ($index + 1);
  2211.         }
  2212.     }
  2213. }
  2214.  
  2215. global proc updateLayerEditor()
  2216. //
  2217. //    Description:
  2218. //        Update the Layer Editor.
  2219. //
  2220. //        This procedure must be called whenever the state of layers in
  2221. //        the scene changes. For example, a new scene is created, or a 
  2222. //        scene is opened, or a layer is added or removed.
  2223. //
  2224. {
  2225.     updateLayersByType("Display");
  2226.     updateLayersByType("Render");
  2227. }
  2228.  
  2229. global proc int layerEditorVisibilityStateChange(
  2230.     int    $newState,
  2231.     string $layout)
  2232. //
  2233. //    Description:
  2234. //        This procedure is called whenever the visibility state of the 
  2235. //        Layer Editor is changed.
  2236. //
  2237. //    Arguments:
  2238. //        newState - The new visibile state of the Layer Editor.
  2239. //
  2240. //        layout - The parent layout for the Layer Editor.
  2241. //
  2242. //    Returns:
  2243. //        true - If the change of state is to be allowed.
  2244. //
  2245. //        false - If the state change is rejected.
  2246. //
  2247. {
  2248.     int $result = true;
  2249.  
  2250.     //    Defer these commands because this proc is called when the visibility
  2251.     //    state is about to change. This proc must return true to accept 
  2252.     //    the state change. After this proc returns then restore the
  2253.     //    panel focus and update the pref menu.
  2254.     //
  2255.     evalDeferred("restoreLastPanelWithFocus(); updatePrefsMenu()");
  2256.  
  2257.     return $result;
  2258. }
  2259.  
  2260. //
  2261. //    Description:
  2262. //        Create the Layer Editor UI.
  2263. //
  2264. {
  2265.     global string $gLayerEditorForm;
  2266.  
  2267.     //    Create the Layer Editor.
  2268.     //
  2269.     createLayerEditor($gLayerEditorForm);
  2270.  
  2271.     //    Update the Layer Editor.
  2272.     //
  2273.     updateLayerEditor();
  2274. }
  2275.